package com.appspot.smartgan;

import java.util.Collections;
import java.util.LinkedList;
import java.util.List;

import org.json.JSONException;
import org.json.JSONObject;

import com.appspot.smartgan.adapter.ContactListAdapter;
import com.appspot.smartgan.server.Client;
import com.appspot.smartgan.server.HttpClient;
import com.appspot.smartgan.server.UrlGenerator;
import com.appspot.smartgan.util.ContactListPerson;
import com.twotoasters.jazzylistview.JazzyListView;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

public class ContactListFragment extends Fragment {
	
	private static String FIRST_PARENT_NAME = "parent1_first_name";
	
	private static String FIRST_PARENT_PHONE = "parent1_phone_number";
	
	private static String SECOND_PARENT_NAME = "parent2_first_name";
	
	private static String SECOND_PARENT_PHONE = "parent2_phone_number";
	
	private String schoolId;
	
	private List<ContactListPerson> list;
	
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		View rootView = 
				inflater.inflate(R.layout.fragment_contact_list, container, false);
		
		if (list == null) {
			return rootView;
		}
	
		// create the JazzyListView
		JazzyListView listView = 
				(JazzyListView) rootView.findViewById(R.id.contact_list);
		listView.setAdapter(new ContactListAdapter(getActivity(), list));
		
		// return the view
		return rootView;
	}

	/* (non-Javadoc)
	 * @see android.support.v4.app.Fragment#onCreate(android.os.Bundle)
	 */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		list = getContactList();
		if (list == null) {
			return;
		}
		
		Collections.sort(list);
	}

	public void setSchool(String schoolId) {
		this.schoolId = schoolId;
	}
	
	private List<ContactListPerson> getContactList() {
		Log.d("SMARTGAN", "getting contactlist from server");
		List<ContactListPerson> lst = new LinkedList<ContactListPerson>();
		
		String url = UrlGenerator.getContactList(schoolId);
		Client client = new HttpClient(url, Client.RESPONSE_ID_LIST);
		client.execute();
		
		String[] ids = (String[]) client.getResponse();
		
		try {
			JSONObject json = null;
			
			for (String id : ids) {
				url = UrlGenerator.getChildInfo(id);
				client = new HttpClient(url, Client.RESPONSE_JSON);
				client.execute();
				
				json = (JSONObject) client.getResponse();

				ContactListPerson person = 
						new ContactListPerson(json.getString(ChildInfoFragment.FIRST_NAME)
								+ " " + json.getString(ChildInfoFragment.LAST_NAME), 
								json.getString(ChildInfoFragment.ADDRESS), 
								json.getString(ChildInfoFragment.PHONE));
				
				if (json.has(FIRST_PARENT_NAME) && json.has(FIRST_PARENT_PHONE)) {
					person.setFirstParentName(json.getString(FIRST_PARENT_NAME));
					person.setFirstParentPhone(json.getString(FIRST_PARENT_PHONE));
				}
				
				if (json.has(SECOND_PARENT_NAME) && json.has(SECOND_PARENT_PHONE)) {
					person.setSecondParentName(json.getString(SECOND_PARENT_NAME));
					person.setSecondParentPhone(json.getString(SECOND_PARENT_PHONE));
				}
				
				lst.add(person);
			}
		} catch (JSONException e) {
			Log.e("SMARTGAN", e.getMessage());
			return lst;
		}
		
		return lst;
	}
	
}
